//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.baidu.lbsapi.auth.tracesdk;

import android.annotation.SuppressLint;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.net.InetSocketAddress;
import java.net.MalformedURLException;
import java.net.Proxy;
import java.net.URL;
import java.net.URLEncoder;
import java.net.Proxy.Type;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map.Entry;
import javax.net.ssl.HttpsURLConnection;

public class g {
    private Context a;
    private String b = null;
    private HashMap c = null;
    private String d = null;

    public g(Context var1) {
        this.a = var1;
    }

    protected String a(HashMap var1) {
        this.c = this.c(var1);
        this.b = (String)this.c.get("url");
        HttpsURLConnection var2 = this.b();
        if(var2 == null) {
            com.baidu.lbsapi.auth.tracesdk.a.c("syncConnect failed,httpsURLConnection is null");
            return this.d;
        } else {
            this.a(var2);
            return this.d;
        }
    }

    protected boolean a() {
        com.baidu.lbsapi.auth.tracesdk.a.a("checkNetwork start");

        try {
            @SuppressLint("WrongConstant")
            ConnectivityManager var1 = (ConnectivityManager)this.a.getSystemService("connectivity");
            if(var1 == null) {
                return false;
            }

            NetworkInfo var3;
            if((var3 = var1.getActiveNetworkInfo()) == null || !var3.isAvailable()) {
                return false;
            }
        } catch (Exception var2) {
            if(com.baidu.lbsapi.auth.tracesdk.a.a) {
                var2.printStackTrace();
            }

            return false;
        }

        com.baidu.lbsapi.auth.tracesdk.a.a("checkNetwork end");
        return true;
    }

    private HttpsURLConnection b() {
        try {
            URL var1 = new URL(this.b);
            com.baidu.lbsapi.auth.tracesdk.a.a("https URL: " + this.b);
            String var2 = this.a(this.a);
            if(var2 != null && !var2.equals("")) {
                com.baidu.lbsapi.auth.tracesdk.a.a("checkNetwork = " + var2);
                HttpsURLConnection var5;
                Proxy var6;
                if(var2.equals("cmwap")) {
                    var6 = new Proxy(Type.HTTP, new InetSocketAddress("10.0.0.172", 80));
                    var5 = (HttpsURLConnection)var1.openConnection(var6);
                } else if(var2.equals("ctwap")) {
                    var6 = new Proxy(Type.HTTP, new InetSocketAddress("10.0.0.200", 80));
                    var5 = (HttpsURLConnection)var1.openConnection(var6);
                } else {
                    var5 = (HttpsURLConnection)var1.openConnection();
                }

                var5.setDoInput(true);
                var5.setDoOutput(true);
                var5.setRequestMethod("POST");
                var5.setConnectTimeout('썐');
                var5.setReadTimeout('썐');
                return var5;
            } else {
                com.baidu.lbsapi.auth.tracesdk.a.c("Current network is not available.");
                this.d = ErrorMessage.a(-10, "Current network is not available.");
                return null;
            }
        } catch (MalformedURLException var3) {
            if(com.baidu.lbsapi.auth.tracesdk.a.a) {
                var3.printStackTrace();
                com.baidu.lbsapi.auth.tracesdk.a.a(var3.getMessage());
            }

            this.d = ErrorMessage.a(-11, "Auth server could not be parsed as a URL.");
            return null;
        } catch (Exception var4) {
            if(com.baidu.lbsapi.auth.tracesdk.a.a) {
                var4.printStackTrace();
                com.baidu.lbsapi.auth.tracesdk.a.a(var4.getMessage());
            }

            this.d = ErrorMessage.a(-11, "Init httpsurlconnection failed.");
            return null;
        }
    }

    private String a(Context var1) {
        String var2 = "wifi";

        try {
            @SuppressLint("WrongConstant")
            ConnectivityManager var4 = (ConnectivityManager)var1.getSystemService("connectivity");
            if(var4 == null) {
                return null;
            } else {
                NetworkInfo var5;
                if((var5 = var4.getActiveNetworkInfo()) != null && var5.isAvailable()) {
                    String var6;
                    if((var6 = var5.getExtraInfo()) != null && (var6.trim().toLowerCase().equals("cmwap") || var6.trim().toLowerCase().equals("uniwap") || var6.trim().toLowerCase().equals("3gwap") || var6.trim().toLowerCase().equals("ctwap"))) {
                        if(var6.trim().toLowerCase().equals("ctwap")) {
                            var2 = "ctwap";
                        } else {
                            var2 = "cmwap";
                        }
                    }

                    return var2;
                } else {
                    return null;
                }
            }
        } catch (Exception var3) {
            if(com.baidu.lbsapi.auth.tracesdk.a.a) {
                var3.printStackTrace();
            }

            return null;
        }
    }

    private void a(HttpsURLConnection var1) {
        com.baidu.lbsapi.auth.tracesdk.a.a("https Post start,url:" + this.b);
        if(this.c == null) {
            this.d = ErrorMessage.a("httpsPost request paramters is null.");
        } else {
            OutputStream var2 = null;
            int var3 = -1;
            boolean var4 = true;

            try {
                BufferedReader var5 = null;
                var2 = var1.getOutputStream();
                BufferedWriter var6;
                (var6 = new BufferedWriter(new OutputStreamWriter(var2, "UTF-8"))).write(b(this.c));
                com.baidu.lbsapi.auth.tracesdk.a.a(b(this.c));
                var6.flush();
                var6.close();
                var1.connect();
                InputStream var34 = null;

                try {
                    var34 = var1.getInputStream();
                    var3 = var1.getResponseCode();
                    if(200 == var3) {
                        var5 = new BufferedReader(new InputStreamReader(var34, "UTF-8"));
                        StringBuffer var8 = new StringBuffer();

                        int var7;
                        while((var7 = var5.read()) != -1) {
                            var8.append((char)var7);
                        }

                        this.d = var8.toString();
                    }
                } catch (IOException var28) {
                    if(com.baidu.lbsapi.auth.tracesdk.a.a) {
                        var28.printStackTrace();
                        com.baidu.lbsapi.auth.tracesdk.a.a("httpsPost parse failed;" + var28.getMessage());
                    }

                    this.d = ErrorMessage.a(-11, "httpsPost failed,IOException:" + var28.getMessage());
                    var4 = false;
                } finally {
                    if(var34 != null && var5 != null) {
                        var5.close();
                        var34.close();
                    }

                    if(var1 != null) {
                        var1.disconnect();
                    }

                }
            } catch (MalformedURLException var30) {
                if(com.baidu.lbsapi.auth.tracesdk.a.a) {
                    var30.printStackTrace();
                }

                this.d = ErrorMessage.a(-11, "httpsPost failed,MalformedURLException:" + var30.getMessage());
                var4 = false;
            } catch (IOException var31) {
                if(com.baidu.lbsapi.auth.tracesdk.a.a) {
                    var31.printStackTrace();
                }

                this.d = ErrorMessage.a(-11, "httpsPost failed,IOException:" + var31.getMessage());
                var4 = false;
            } catch (Exception var32) {
                if(com.baidu.lbsapi.auth.tracesdk.a.a) {
                    var32.printStackTrace();
                }

                this.d = ErrorMessage.a(-11, "httpsPost failed,Exception:" + var32.getMessage());
                var4 = false;
            } finally {
                if(var2 != null) {
                    try {
                        var2.close();
                    } catch (IOException var27) {
                        if(com.baidu.lbsapi.auth.tracesdk.a.a) {
                            var27.printStackTrace();
                        }
                    }
                }

            }

            if(var4 && 200 != var3) {
                com.baidu.lbsapi.auth.tracesdk.a.a("httpsPost failed,statusCode:" + var3);
                this.d = ErrorMessage.a(-11, "httpsPost failed,statusCode:" + var3);
            } else if(this.d == null) {
                com.baidu.lbsapi.auth.tracesdk.a.a("httpsPost failed,mResult is null");
                this.d = ErrorMessage.a(-1, "httpsPost failed,internal error");
            } else {
                com.baidu.lbsapi.auth.tracesdk.a.a("httpsPost success end,parse result = " + this.d);
            }
        }
    }

    private static String b(HashMap var0) throws UnsupportedEncodingException {
        StringBuilder var1 = new StringBuilder();
        boolean var2 = true;
        Iterator var4 = var0.entrySet().iterator();

        while(var4.hasNext()) {
            Entry var3 = (Entry)var4.next();
            if(var2) {
                var2 = false;
            } else {
                var1.append("&");
            }

            var1.append(URLEncoder.encode((String)var3.getKey(), "UTF-8"));
            var1.append("=");
            var1.append(URLEncoder.encode((String)var3.getValue(), "UTF-8"));
        }

        return var1.toString();
    }

    private HashMap c(HashMap var1) {
        HashMap var2 = new HashMap();
        Iterator var3 = var1.keySet().iterator();

        while(var3.hasNext()) {
            String var4 = ((String)var3.next()).toString();
            var2.put(var4, var1.get(var4));
        }

        return var2;
    }
}
